<template>
  <web-view
    :src="`${h5Address}/${path}?token=${$store.state.user.token}`"
    @message="onMessage"
  ></web-view>
</template>

<script>
import { h5Address } from '@/env'
export default {
  name: 'Backend',
  data() {
    return { h5Address, path: '' }
  },
  onLoad(options) {
    this.path = options.path || ''
		let { token } = this.$store.state.user
		// console.log('token', JSON.stringify(this.$store.state.user))
		if (!token || token === '') {
        uni.redirectTo({ url: '/pages/login/login' })
		}
    // uni.setNavigationBarTitle({ title: '新的标题' })
  },
  methods: {
    onMessage(e) {
      console.log(JSON.stringify(e), 'returnApp')
      // 接收webview发送的消息
      const data = e.detail.data[0]
      console.log(JSON.stringify(data), 'data returnApp')
      if (data.action === 'returnApp') {
        // 当webview想要返回app时，调用uni的返回方法
        uni.navigateBack()
        return
      }
      if(data.action === 'returnAppHome'){
        uni.switchTab({ url: '/pages/home/index' })
      }
      if(data.url){
        uni.navigateTo({url: data.url})
      }
    }
  }
}
</script>

<style></style>
